<template>
<div>
    <div>
        <el-page-header class="head" @back="goBack" :content="content">
        </el-page-header>
    </div>
    <div >
            <el-form  label-width="80px" >
              
                <el-form-item label="账号">
                <el-input v-model="username" ></el-input>
                </el-form-item>

                <el-form-item label="密码">
                  <el-input v-model="password" ></el-input>
                </el-form-item>

                <el-form-item label="姓名">
                    <el-input v-model="nickname" ></el-input>
                </el-form-item>
                
              <el-button @click="creatUser" class="button" >立即创建</el-button>
          </el-form>
      
    </div>
    
</div>
</template>
<script>
export default{
    data() {
        return {
            username:'',
            password:'',
            nickname:'',
            content:'',
        }
    },
   async created() {
      let id=this.$route.params.id;
      if(id){
        this.content="编辑用户页面"
        let res=await this.$http('user/get?id='+id);
        this.username=res.data.username;
        this.password=res.data.password;
        this.nickname=res.data.nickname;
        console.log(res);
      }else{
        this.content="新增用户页面"
      }
    },
    methods: {
        goBack() {
        javascript:history.back(-1);
      },
       async creatUser(){
         if(this.$route.query.id){
          let res = await this.$http.post('/user/update', {
            username: this.username,
            password: this.password,
            nickname:this.nickname,
            id:this.$route.query.id,
            })
         }else{
          let res = await this.$http.post('/user/insert', {
            username: this.username,
            password: this.password,
            nickname:this.nickname,
            })
            console.log(2);
            console.log(res);
         }
         this.$router.push('/User/List')
      },
      
    },
}
</script>
<style scoped>
.head{
    margin: 20px;
}
.button{
    background: #008080;
    color: white;
    margin-left:80px;
  }
</style>